define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'saler/group/index',
                    add_url: 'saler/group/add',
                    edit_url: 'saler/group/edit',
                    del_url: 'saler/group/del',
                    multi_url: 'saler/group/multi',
                    table: 'saler_group',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                pageSize: 20,
                pageList: [20, 50],
                clickToSelect: false,
                dblClickToEdit: false,
                singleSelect: false,
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'name',
                            title: __('Name')
                        },
                        // {field: 'admin_id', title: __('Admin_id'), visible: false},
                        {
                            field: 'count',
                            title: __('Count')
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        // {
                        //     field: 'updatetime',
                        //     title: __('Updatetime'),
                        //     operate: 'RANGE',
                        //     addclass: 'datetimerange',
                        //     formatter: Table.api.formatter.datetime,
                        //     visible: false
                        // },
                        // {
                        //     field: 'admin.id',
                        //     title: __('Admin.id'),
                        //     visible: false
                        // },
                        {
                            field: 'admin.nickname',
                            title: __('销售代表')
                        },
                        {
                            field: 'id',
                            title: __('群组管理'),
                            formatter: Controller.api.formatter.id,
                            operate: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                id: function (value, row, index) {
                    url = 'saler/groupinfo?saler_group_id=' + value;
                    return '<a href="' + url + '" class="label label-info addtabsit" title="' + __("", value) + '">' + __('群组用户管理', value) + '</a>';
                }
            }
        }
    };
    return Controller;
});